<template>
  <div class="common-page f-c">
    <BackPage title="Banner管理" />
    <div class="search-params f-r a-c j-e">
      <el-form inline>
        <el-form-item>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-box f-g">
      <el-table
        height="100%"
        v-loading="loading"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" width="80px" label="序号" align="center" />
        <el-table-column label="banner图" align="center">
          <template slot-scope="scope">
            <el-image :src="scope.row.img" style="width: 189px;height: 76px" :preview-src-list="[scope.row.img]" />
          </template>
        </el-table-column>
        <el-table-column label="跳转地址" align="center">
          <template slot-scope="scope">
            <el-link :href="scope.row.link" target="_blank">
                {{ scope.row.link}}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="上下架">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :inactive-value="1" :active-value="0" @change="(val) => onUpdateStatus(val, scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="位置选择">
          <template slot-scope="scope">
            <el-select clearable v-model="scope.row.type">
              <el-option v-for="item in options" :key="item.value" :value="item.value" :label="item.name" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="80px">
          <template slot-scope="scope">
            <el-button type="text" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="上传banner"
      :visible.sync="dialogVisible"
      width="540px"
    >
      <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input clearable v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="跳转地址" prop="link">
          <el-input clearable v-model="ruleForm.link" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select clearable v-model="ruleForm.type">
            <el-option v-for="item in options" :key="item.value" :value="item.value" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <DragUpload :url="ruleForm.img" @on-success="url => ruleForm.img = url" />
          <div style="font-size: 14px;color: #666;">只能上传jpg/png文件，且不超过500kb</div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="ruleForm.status" :inactive-value="1" :active-value="0" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http_banner } from '@/api'
export default {
  name: 'Index',
  mixins: [crud],
  data() {
    return {
      request: http_banner,
      dialogVisible: false,
      searchParams: {
      },
      options: [
        { value: 1, name: '浙里办首页' }
      ],
      tableData: [
      ],
      ruleForm: {
        title: '',
        img: '',
        type: '',
        link: '',
        status: 0
      },
      rules: {
        title: [{ required: true, message: '不能为空' }],
        type: [{ required: true, message: '不能为空' }],
        img: [{ required: true, message: '不能为空' }]
      }
    }
  },
  methods: {
    onAdd() {
      this.dialogVisible = true
    },
    onUpdateStatus(status, item) {
      http_banner.updateStatus({
        id: item.id,
        status
      }).then(res => {
        this.$message.success(res.msg)
      })
    },
    onSave() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          http_banner.create({ ...this.ruleForm }).then(res => {
            this.$message.success(res.msg)
            this.onSearch()
            this.dialogVisible = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
